﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class ImportForeignLogs : Form
    {
        public ImportForeignLogs()
        {
            InitializeComponent();
        }

        private EventScavengerDAL eventScavengerDAL;

        #region Public methods
        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        } 
        #endregion

        #region Form events
        private void ImportForeignLogs_Load(object sender, EventArgs e)
        {
            lvwImportLogs.Items.Add("Loading...");
            backgroundWorkerLoading.RunWorkerAsync();
        }
        #endregion

        #region Loading
        private ListViewGroup GetListViewGroupByName(string name)
        {
            ListViewGroup group = null;
            foreach (ListViewGroup lvg in lvwImportLogs.Groups)
            {
                if (lvg.Name.ToUpper() == name.ToUpper())
                {
                    group = lvg;
                    break;
                }
            }
            if (group == null)
            {
                group = new ListViewGroup(name.ToUpper(), name.ToUpper());
                lvwImportLogs.Groups.Add(group);
            }
            return group;
        }
        private void backgroundWorkerLoading_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread.Sleep(500);
            this.Invoke((MethodInvoker)delegate()
            {
                List<ImportForeignLog> importLogs = eventScavengerDAL.GetImportLogs();
                Application.DoEvents();
                lvwImportLogs.Items.Clear();
                foreach (ImportForeignLog importLog in importLogs)
                {
                    ListViewItem lvi = new ListViewItem(importLog.SQLServer);
                    lvi.Group = GetListViewGroupByName(importLog.CollectorName);
                    if (!importLog.Enabled)
                    {
                        lvi.ForeColor = Color.DarkGray;
                    }
                    lvi.SubItems.Add(importLog.Database);
                    lvi.SubItems.Add(importLog.Machine);
                    lvi.SubItems.Add(importLog.LogName);
                    lvi.Tag = importLog;
                    lvwImportLogs.Items.Add(lvi);
                }
            });
        } 
        #endregion

        #region Listview events
        private void lvwImportLogs_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolStripButtonEdit.Enabled = lvwImportLogs.SelectedItems.Count == 1;
            editToolStripMenuItem.Enabled = lvwImportLogs.SelectedItems.Count == 1;
            toolStripButtonRemove.Enabled = lvwImportLogs.SelectedItems.Count > 0;
            deleteToolStripMenuItem.Enabled = lvwImportLogs.SelectedItems.Count > 0;
            bool entryEnable = false;
            bool entryNotEnable = false;
            foreach (ListViewItem lvi in lvwImportLogs.SelectedItems)
            {
                if (lvi.ForeColor == Color.DarkGray)
                    entryEnable = true;
                else
                    entryNotEnable = true;
            }
            toolStripButtonEnable.Enabled = entryEnable;
            toolStripButtonDisable.Enabled = entryNotEnable;
        } 
        private void lvwImportLogs_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                toolStripButtonEdit_Click(sender, e);
            }
        }
        #endregion

        #region Toolbar button events
        private void toolStripButtonNew_Click(object sender, EventArgs e)
        {
            try
            {
                ImportForeignLogEdit importForeignLogEdit = new ImportForeignLogEdit();
                importForeignLogEdit.SelectedImportForeignLog = new ImportForeignLog();
                if (importForeignLogEdit.Show(eventScavengerDAL) == DialogResult.OK)
                {
                    eventScavengerDAL.InsertImportForeignLog(importForeignLogEdit.SelectedImportForeignLog);
                    backgroundWorkerLoading.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "New", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwImportLogs.SelectedItems.Count > 0)
                {
                    ImportForeignLogEdit importForeignLogEdit = new ImportForeignLogEdit();
                    importForeignLogEdit.SelectedImportForeignLog = (ImportForeignLog)lvwImportLogs.SelectedItems[0].Tag;
                    if (importForeignLogEdit.Show(eventScavengerDAL) == DialogResult.OK)
                    {
                        eventScavengerDAL.UpdateImportForeignLog(importForeignLogEdit.SelectedImportForeignLog);
                        ListViewItem lvi = lvwImportLogs.SelectedItems[0];
                        lvi.Group = GetListViewGroupByName(importForeignLogEdit.SelectedImportForeignLog.CollectorName);
                        lvi.Text = importForeignLogEdit.SelectedImportForeignLog.SQLServer;                        
                        lvi.SubItems[1].Text = importForeignLogEdit.SelectedImportForeignLog.Database;
                        lvi.SubItems[2].Text = importForeignLogEdit.SelectedImportForeignLog.Machine;
                        lvi.SubItems[3].Text = importForeignLogEdit.SelectedImportForeignLog.LogName;
                        lvi.Tag = importForeignLogEdit.SelectedImportForeignLog;
                        if (!importForeignLogEdit.SelectedImportForeignLog.Enabled)
                        {
                            lvi.ForeColor = Color.DarkGray;
                        }
                        else
                            lvi.ForeColor = SystemColors.WindowText;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Edit", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void toolStripButtonRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvwImportLogs.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show("Are you sure you want to delete this entry(s)?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        foreach (ListViewItem lvi in lvwImportLogs.SelectedItems)
                        {
                            ImportForeignLog importForeignLog = (ImportForeignLog)lvi.Tag;
                            eventScavengerDAL.DeleteImportForeignLog(importForeignLog);
                        }
                    }
                    backgroundWorkerLoading.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        } 
        private void toolStripButtonEnable_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem lvi in lvwImportLogs.SelectedItems)
                {
                    ImportForeignLog importForeignLog = (ImportForeignLog)lvi.Tag;
                    importForeignLog.Enabled = true;
                    eventScavengerDAL.UpdateImportForeignLog(importForeignLog);
                    lvi.Tag = importForeignLog;
                    lvi.ForeColor = SystemColors.WindowText;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Enable", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void toolStripButtonDisable_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem lvi in lvwImportLogs.SelectedItems)
                {
                    ImportForeignLog importForeignLog = (ImportForeignLog)lvi.Tag;
                    importForeignLog.Enabled = false;
                    eventScavengerDAL.UpdateImportForeignLog(importForeignLog);
                    lvi.Tag = importForeignLog;
                    lvi.ForeColor = Color.DarkGray;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Disable", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

    }
}
